package com.chuji.crmcj.act;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.view.CalendarView;
import com.chuji.crmcj.view.CalendarViewDialog;
import com.chuji.crmcj.view.NiceSpinner;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WorkFormActivity extends Activity{
    //返回
    private ImageView iv_Back;
    //下拉框
    private NiceSpinner spinner;
    //数据
    String [] forms={"选择报表类型","客户接待","客户跟进","成交量","战败率"};
    //适配器
    ArrayAdapter<String> adapter;
    //日期
    private TextView tv_data;
    //日历图片
    private ImageView iv_calendar;
    //日历控件
    public CalendarView calendar;
    //显示下拉列表
    private PopupWindow popupwindow;
    //报表
    private LinearLayout ll_forms;
    private RelativeLayout Rl_title;
    WindowManager windowManager;
    CalendarViewDialog dialog;
    WindowManager.LayoutParams lp;
    private LinearLayout mLl_client_receive;
    private LinearLayout mLl_client_follow;
    private LinearLayout mLl_volume;
    private LinearLayout mLl_lose_rate;
//    CalendarViewDialog.OnSureClickListener mListener;

    @Override
    protected void onStart() {
        super.onStart();
        iv_calendar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog = new CalendarViewDialog(WorkFormActivity.this);
                dialog.show();
                dialog.calendar.setOnItemClickListener(new CalendarView.OnItemClickListener() {
                    @Override
                    public void OnItemClick(Date selectedStartDate, Date selectedEndDate, Date downDate) {
                        if(dialog.calendar.isSelectMore()){
                            UIUtils.showToastSafe(dialog.format.format(selectedStartDate) + "到" + dialog.format.format(selectedEndDate));
                        }else{
                            tv_data.setText(dialog.format.format(downDate));
                            dialog.dismiss();
                        }
                    }
                });
                windowManager = getWindowManager();
                lp = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
                //p.height = (int) (d.getHeight() * 0.3);   //高度设置为屏幕的0.3
                lp.width = windowManager.getDefaultDisplay().getWidth();//为获取屏幕宽、高,宽度设置为全屏
                dialog.getWindow().setAttributes(lp);     //设置生效
            }
        });

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_form);
        //得到控件
        tv_data = (TextView) findViewById(R.id.tv_data);
        iv_calendar = (ImageView) findViewById(R.id.iv_calendar);
        ll_forms = (LinearLayout) findViewById(R.id.ll_forms);
        Rl_title= (RelativeLayout) findViewById(R.id.Rl_title);
        iv_Back = (ImageView) findViewById(R.id.iv_Back);
        //获取当天日期
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy/MM/dd");
        Date curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
        tv_data.setText(formatter.format(curDate));
        //返回
        iv_Back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //选择报表类型
//        ll_forms.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (popupwindow != null && popupwindow.isShowing()) {
//                    popupwindow.dismiss();
//                    return;
//                } else {
//                    initmPopupWindowView();
//                    popupwindow.showAsDropDown(view, 0, 5);
//                }
//            }
//        });
//            //创建adapter对象
//            adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,forms);
//            //填充
//            spinner.setAdapter(adapter);
//            spinner.setSelected(false);
////            添加itemSeleted事件
//            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
//
//                                              {
//                                                  /**
//                                                   * 当元素被选中的时候自动调用
//                                                   * 参数1：parent-->spinner
//                                                   * 参数2：view-->当前子项元素的布局 TextView
//                                                   * 参数3： postion-->当前选中元素的索引
//                                                   * 参数4：当前选中的子项元素的控件在ListView中索引位置
//                                                   */
//                                                  @Override
//                                                  public void onItemSelected(AdapterView<?> parent, View view,
//                                                                             int position, long id) {
//                                                      if (!"选择报表类型".equals(forms[position])) {
//
//                                                      } else {
//
//                                                      }
//                                                  }
//
//                                                  /**
//                                                   * 没有选中。。自动调用
//                                                   */
//                                                  @Override
//                                                  public void onNothingSelected(AdapterView<?> parent) {
//                                                      // TODO Auto-generated method stub
//
//                                                  }
//                                              }
//
//            );
//
        }
    public void initmPopupWindowView() {
        // // 获取自定义布局文件pop.xml的视图
        View customView = getLayoutInflater().inflate(R.layout.popview_item,
                null, false);
        // 创建PopupWindow实例,WindowManager.LayoutParams.MATCH_PARENT,200分别是宽度和高度
        popupwindow = new PopupWindow(customView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupwindow.setBackgroundDrawable(new ColorDrawable(0xc0000000));
        // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
        popupwindow.setAnimationStyle(R.style.AnimationFade);
        backgroundAlpha(0.5f);
        popupwindow.showAsDropDown(Rl_title);
        //初始化数据
        mLl_client_receive = (LinearLayout)customView.findViewById(R.id.ll_client_receive);
        mLl_client_follow = (LinearLayout) customView.findViewById(R.id.ll_client_follow);
        mLl_volume = (LinearLayout) customView.findViewById(R.id.ll_volume);
        mLl_lose_rate = (LinearLayout) customView.findViewById(R.id.ll_lose_rate);
        /** 在这里可以实现自定义视图的功能 */
        mLl_client_receive.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.showToastSafe("显示第一个");

                popupwindow.dismiss();
            }
        });
        mLl_client_follow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.showToastSafe("显示第二个");
                popupwindow.dismiss();
            }
        });
        mLl_volume.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.showToastSafe("显示第三个");
                popupwindow.dismiss();
            }
        });
        mLl_lose_rate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.showToastSafe("显示第四个");
                popupwindow.dismiss();
            }
        });
        //popWindow消失监听方法
        popupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
}
